import apiClient from '@/api/apiClient.ts';
import { MenuVoInfo, RoleInfo } from '#/entity.ts';

enum RoleServices {
  GetRolesByUserId = '/roadCure/role/getRolesByUserId',
  GetRoleDetail = '/roadCure/role',
  GetMenuList = '/roadCure/multi/getMenuList',
  SaveRole = '/roadCure/role/save',
  UpdateRole = '/roadCure/role/update',
  AssignMenu = '/roadCure/multi/assignMenu',
  DeleteRole = '/roadCure/role/delete',
}

const getRolesByUserId = (params: { userId: string | number }) =>
  apiClient.get<RoleInfo[]>({ url: RoleServices.GetRolesByUserId, params });

const getRoleDetail = (userId: number) =>
  apiClient.get<RoleInfo>({
    url: `${RoleServices.GetRoleDetail}/${userId}`,
  });

const getMenuList = () =>
  apiClient.get<MenuVoInfo[]>({
    url: RoleServices.GetMenuList,
  });

const saveRole = (params: Pick<RoleInfo, 'userId' | 'roleName' | 'roleDesc'>) =>
  apiClient.post<Omit<RoleInfo, 'menuVoList'>>({
    url: RoleServices.SaveRole,
    data: params,
  });

const updateRole = (params: Omit<RoleInfo, 'menuVoList'>) =>
  apiClient.put<boolean>({
    url: RoleServices.UpdateRole,
    data: params,
  });

const assignMenu = (params: { roleId: number; menuPaths: string[]; areaCode: string }[]) =>
  apiClient.post<boolean>({
    url: RoleServices.AssignMenu,
    data: params,
  });

const deleteRole = (id: number) =>
  apiClient.delete<boolean>({ url: `${RoleServices.DeleteRole}/${id}` });

export default {
  getRolesByUserId,
  getRoleDetail,
  getMenuList,
  saveRole,
  updateRole,
  deleteRole,
  assignMenu,
};
